#!/bin/bash

# Android 应用遍历测试脚本
# 用于运行所有 UI 自动化测试

set -e

echo "=========================================="
echo "  WeChat Android 应用遍历测试脚本"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查是否在正确的目录
if [ ! -f "app/build.gradle.kts" ]; then
    echo -e "${RED}❌ 错误: 请在 android 项目根目录运行此脚本${NC}"
    exit 1
fi

# 检查设备连接
echo "📱 检查设备连接..."
if command -v adb &> /dev/null; then
    DEVICES=$(adb devices | grep -v "List" | grep "device$" | wc -l | tr -d ' ')
    if [ "$DEVICES" -eq 0 ]; then
        echo -e "${YELLOW}⚠️  未找到连接的设备${NC}"
        echo "请连接 Android 设备或启动模拟器"
        echo ""
        read -p "是否继续？(y/n) " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            exit 1
        fi
    else
        echo -e "${GREEN}✅ 找到 $DEVICES 个设备${NC}"
        adb devices
    fi
else
    echo -e "${YELLOW}⚠️  adb 未找到，无法检查设备${NC}"
    echo "请确保 Android SDK platform-tools 在 PATH 中"
fi
echo ""

# 检查应用是否已安装
echo "📦 检查应用安装状态..."
PACKAGE_NAME="com.wechat.app"
if command -v adb &> /dev/null; then
    INSTALLED=$(adb shell pm list packages | grep "$PACKAGE_NAME" | wc -l | tr -d ' ')
    if [ "$INSTALLED" -eq 0 ]; then
        echo -e "${YELLOW}⚠️  应用未安装${NC}"
        echo "正在安装应用..."
        ./gradlew installDebug
    else
        echo -e "${GREEN}✅ 应用已安装${NC}"
    fi
else
    echo -e "${YELLOW}⚠️  无法检查应用安装状态${NC}"
fi
echo ""

# 显示测试选项
echo "请选择测试类型:"
echo "1. 运行所有测试"
echo "2. 运行导航测试 (AppNavigationTest)"
echo "3. 运行聊天流程测试 (ChatFlowTest)"
echo "4. 运行通讯录测试 (ContactsFlowTest)"
echo "5. 运行完整遍历测试 (FullAppTraversalTest)"
echo "6. 运行搜索功能测试"
echo "7. 运行 UiAutomator 遍历测试 (推荐，兼容 API 36)"
echo "8. 自定义测试类"
echo ""
read -p "请输入选项 (1-8): " choice

case $choice in
    1)
        echo ""
        echo -e "${GREEN}🚀 运行所有测试...${NC}"
        ./gradlew connectedAndroidTest
        ;;
    2)
        echo ""
        echo -e "${GREEN}🚀 运行导航测试...${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.AppNavigationTest"
        ;;
    3)
        echo ""
        echo -e "${GREEN}🚀 运行聊天流程测试...${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.ChatFlowTest"
        ;;
    4)
        echo ""
        echo -e "${GREEN}🚀 运行通讯录测试...${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.ContactsFlowTest"
        ;;
    5)
        echo ""
        echo -e "${GREEN}🚀 运行完整遍历测试...${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.FullAppTraversalTest"
        ;;
    6)
        echo ""
        echo -e "${GREEN}🚀 运行搜索功能测试...${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.FullAppTraversalTest.testSearchFunctionality"
        ;;
    7)
        echo ""
        echo -e "${GREEN}🚀 运行 UiAutomator 遍历测试 (推荐)...${NC}"
        echo -e "${YELLOW}注意: 这个测试使用 UiAutomator，在 Android API 36 上更稳定${NC}"
        ./gradlew connectedAndroidTest --tests "com.wechat.app.UIAutomatorTraversalTest"
        ;;
    8)
        echo ""
        read -p "请输入测试类名 (例如: com.wechat.app.AppNavigationTest): " test_class
        echo -e "${GREEN}🚀 运行自定义测试: $test_class${NC}"
        ./gradlew connectedAndroidTest --tests "$test_class"
        ;;
    *)
        echo -e "${RED}❌ 无效选项${NC}"
        exit 1
        ;;
esac

echo ""
echo "=========================================="
echo "  测试完成"
echo "=========================================="

# 显示测试报告位置
if [ -d "app/build/outputs/androidTest-results" ]; then
    echo ""
    echo "📊 测试报告位置:"
    echo "   app/build/outputs/androidTest-results/"
    echo ""
    echo "查看详细报告:"
    echo "   find app/build/outputs/androidTest-results -name '*.xml'"
fi

